package com.gm.wholesale.db.order;
import com.gm.commonlib.util.jsonUtils.JsonUtils;
import com.gm.wholesale.db.DatabaseHelper;
import com.gm.wholesale.db.order.bean.DBOrderBean;
import com.gm.wholesale.db.order.bean.DBOrderContentBean;
import com.gm.wholesale.module.custom.ChoiceCustomData;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.user.UserDataManger;
import com.gm.wholesale.util.AppUtil;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.youth.banner.util.LogUtils;

import java.sql.SQLException;
import java.util.List;

public class DBOrderUtil {

    //todo 挂单保存 订单数据
    public static void saveOrder(DatabaseHelper databaseHelper, List<DBOrderContentBean> datas){

         if (databaseHelper!=null){
             Dao<DBOrderBean, Long> DBOrderBeanDao = databaseHelper.getDBOrderBeanDao();
             DBOrderBean dbOrderBean=new DBOrderBean();
             dbOrderBean.setUnionId(UserDataManger.newInstance().getUId());
             dbOrderBean.setTime(System.currentTimeMillis());
             dbOrderBean.setUserId(UserDataManger.newInstance().getUserId());
             dbOrderBean.setOrderId(AppUtil.getUUid());
             CustomBean customBean= ChoiceCustomData.newInstance().getCustomBean();
             if (customBean!=null){
                 //todo other1 用来保存客户名字，不想更新数据库，加字段了
                 dbOrderBean.setOther1(customBean.getName());
             }

             try {
               DBOrderBeanDao.create(dbOrderBean);
                if (datas==null){
                    return;
                }
                 Dao<DBOrderContentBean, Long> orderDao = databaseHelper.getDBOrderContentDao();
                 for (DBOrderContentBean data:datas){
                     data.setOrderId(dbOrderBean.getOrderId());
                     orderDao.create(data);
                 }
             } catch (SQLException e) {
                 LogUtils.d("SQLException: "+e.toString());
             }

         }
    }
    //todo 获取挂单列表数据
    public static List<DBOrderBean>  getDBOrderBean(DatabaseHelper databaseHelper){
            Dao<DBOrderBean, Long> DBOrderBeanDao = databaseHelper.getDBOrderBeanDao();
        try {
          List<DBOrderBean> orderBeans= DBOrderBeanDao.queryForEq("userId",UserDataManger.newInstance().getUserId());
          return orderBeans;
        } catch (Exception e) {
           LogUtils.d("SQLException : "+e.toString());
           return null;
        }
    }

    //todo 获取挂单条数
    public static int getOrderSize(DatabaseHelper databaseHelper){
        List<DBOrderBean> orderBeans=getDBOrderBean(databaseHelper);
        if (orderBeans!=null){
            return orderBeans.size();
        }
        return 0;
    }

   //todo 每个挂单里面的具体单子数据
    public static List<DBOrderContentBean>  getDBOrderDetailByOrderId(DatabaseHelper databaseHelper,String orderId){

        try {
            Dao<DBOrderContentBean, Long> DBOrderBeanDao = databaseHelper.getDBOrderContentDao();

            List<DBOrderContentBean> orderBeans= DBOrderBeanDao.queryForEq("orderId",orderId);
            List<DBOrderContentBean> orderBeans2= DBOrderBeanDao.queryForAll();
            LogUtils.d("DBOrderContentBeanDBOrderContentBean: "+ JsonUtils.toString(orderBeans2));
            return orderBeans;
        } catch (SQLException e) {
            LogUtils.d("SQLException : "+e.toString());
            return null;
        }
    }

    //todo 删除挂单数据
    public static void deleteOrder(DatabaseHelper databaseHelper,long id){

        try {
            Dao<DBOrderBean, Long> DBOrderBeanDao = databaseHelper.getDBOrderBeanDao();
            Dao<DBOrderContentBean, Long> dao = databaseHelper.getDBOrderContentDao();
            DBOrderBeanDao.deleteById(id);
            DeleteBuilder<DBOrderContentBean, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("id", id);
            deleteBuilder.delete();

        } catch (Exception e) {
        }
    }
}
